package org.kisekiproject.pages.menu {
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;
	
	import org.kisekiproject.components.ui.StateButton;
	import org.kisekiproject.events.KisekiEvent;
	
	public class MenuBase extends MovieClip {

		[Property(arrayType="org.kisekiproject.pages.menu.MenuItem")]
		public var menuitems:Array;

		// FLA
		public var buttonHolder:MovieClip;

		private var _menuItemByButton:Dictionary;

		/**
		 * Constrcutor.
		 */
		public function MenuBase() {
			trace("menu base constructor...");
			addEventListener(KisekiEvent.CREATION_COMPLETE,onCreationComplete);
		}

		/**
		 * Creation complete.
		 */
		private function onCreationComplete(event:KisekiEvent):void {
			var y:int=0;

			_menuItemByButton=new Dictionary();

			for each (var menuItem:MenuItem in menuitems) {
				var button:MovieClip=new MenuButton();

				button.labelField.text=menuItem.label;
				button.y=y;

				y+=100;

				buttonHolder.addChild(button);
				button.addEventListener(MouseEvent.CLICK,onButtonClick);

				_menuItemByButton[button]=menuItem;
			}
		}

		/**
		 * Button click.
		 */
		private function onButtonClick(event:Event):void {
			var menuItem:MenuItem=_menuItemByButton[event.target];

			dispatchEvent(new Event(menuItem.event));
		}
	}
}